#!/usr/bin/env bash
# deploy.sh
#

USERNAME=keatonlao
REPOSITORYNAME=a-study-note-for-touch-typing

# If a command fails then the deploy stops
set -e

printf "\033[0;32mDeploying updates to Gitee...\033[0m"
echo -e "\n\033[0;31;1m+ $REPOSITORYNAME\033[0m"

# ----------------------------------------------
# Functions
pushFun() {
    # Add changes to git.
    git add .
    # Commit changes.
    # msg="Update $(date +"[%x %T]")"
    msg="Initial commit"
    if [ -n "$*" ]; then
        msg="$*"
    fi
    git commit -m "$msg"
    # Push source and build repos.
    git push origin master
}
# ----------------------------------------------


#### Action 1 ####

echo -e "\033[0;34mBuilding web pages... [HTML]\033[0m"
# Go To src folder
cd src
# Running hugo does not remove generated files before building. This means that 
# you should delete your public/ directory (or the publish directory you 
# specified via flag or configuration file) before running the hugo command. If
# you do not remove these files, you run the risk of the wrong files (e.g., 
# drafts or future posts) being left in the generated site.
rm -fr public/*
# Build the project.
hugo --gc --minify 	# if using a theme, replace with `hugo -t <YOURTHEME>`


#### Action 2 ####

echo -e "\n\033[0;32mUpload files to Gitee...\033[0m"
# Come Back up to the Project Root
cd ..
pushFun


#### Action 3 ####

echo -e "\n\033[0;31;1mGitee Pages\033[0m \033[0;34mhttps://gitee.com/help/articles/4136#article-header0\033[0m"
echo -e "\033[0;33mPlease go to Gitee to update the site manually:\033[0m"
echo "  https://gitee.com/$USERNAME/$REPOSITORYNAME/pages"